package com.my.sipt.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import sun.misc.BASE64Decoder;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.DatatypeConverter;
import java.awt.image.BufferedImage;
import java.io.*;

import static jdk.nashorn.internal.objects.Global.undefined;

@RestController
public class AllController {

    @RequestMapping("upload2")
    public void upload(MultipartFile avatar,HttpServletRequest request) throws IOException {

        String uploadFileName = avatar.getOriginalFilename();
        String path ="/www/wwwroot/www.dfusmc.top/DfUSMC-master/Dataset";
        File realPath = new File(path);
        if (!realPath.exists()) {
            realPath.mkdir();
        }
        Logger logger= LoggerFactory.getLogger(AllController.class);
        logger.info("文件上传保存地址：" + realPath);
        InputStream is = avatar.getInputStream();
        File file = new File(realPath +"/"+ uploadFileName);
        FileOutputStream oos = new FileOutputStream(file);
        int len;
        byte[] buffer = new byte[1024];
        while ((len = is.read(buffer)) != -1) {
            oos.write(buffer, 0, len);
            oos.flush();
        }
        oos.close();
        is.close();


    }

    @RequestMapping("makepic")
    public  void makePic(String name){

        System.out.println(name);
        String[] split = name.split("\\.");
        try {
            FileWriter fw = new FileWriter("/www/wwwroot/www.dfusmc.top/DfUSMC-master/test1.sh");
            String str=
                    "cd /www/wwwroot/www.dfusmc.top/DfUSMC-master\n"+
                    "vncserver\n"+
                    "export DISPLAY=localhost:1\n"+
                    "xhost +\n"+
                    "./DfUSMC "+split[0]+" "+split[1];
            fw.write(str);
            fw.close();

            Runtime runtime = Runtime.getRuntime();

            Process process =runtime.exec("sh /www/wwwroot/www.dfusmc.top/DfUSMC-master/test1.sh");
            InputStream in = process.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(in,"UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while ((line= bufferedReader.readLine())!=null) {
                System.out.println(line);
                if(line.contains("confidence threshold")){
                   break;
                }
            }
            bufferedReader.close();
            inputStreamReader.close();
            in.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("showpic")
    public String showPic(String name){
        String[] split = name.split("\\.");
        String ret="http://39.107.232.83/DfUSMC-master/Result/"+split[0]+"_Refined.bmp";
        System.out.println(ret);
        return ret;
    }
}
